package com.lizheng.runner;

import com.lizheng.domain.entity.Article;
import com.lizheng.mapper.ArticleMapper;
import com.lizheng.utils.RedisCache;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {


    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        //查询博客信息 id viewcontent
        List<Article> articles = articleMapper.selectList(null);
        Map<String,Integer> viewCountMap = articles.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(),article ->{
                    return article.getViewCount().intValue();
                }));
        //存储到redis中
        redisCache.setCacheMap("article:viewCountMap",viewCountMap);

    }
}
